package com.tkjt.pms.modules.report.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tkjt.pms.common.entity.TkIndexValueAttrs;
import com.tkjt.pms.modules.common.mapper.TkIndexValueAttrsMapper;
import org.jeecgframework.poi.excel.entity.params.ExcelExportEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.tkjt.pms.common.entity.TkIndexInfo;
import com.tkjt.pms.common.entity.TkIndexValueMirror;
import com.tkjt.pms.modules.report.service.ExcelBuilder;
import com.tkjt.pms.modules.report.service.IndexDataLoader;
import com.tkjt.pms.modules.report.service.common.ExcelBuilderParam;
import com.tkjt.pms.modules.report.service.common.ExcelBuilderResult;
import com.tkjt.pms.modules.report.service.common.ExcelBuilderUtils;
import com.tkjt.pms.modules.report.util.DeptConstant;
import com.tkjt.pms.modules.report.vo.DatagridVO;

@Service("RDPH")
public class RDPHExcelBuilder implements ExcelBuilder {
	
	@Autowired
	private IndexDataLoader indexDataLoader;
	@Autowired
	private TkIndexValueAttrsMapper tkIndexValueAttrsMapper;

	@SuppressWarnings("unchecked")
	@Override
	public ExcelBuilderResult doExcelBuilder(ExcelBuilderParam excelBuilderParam) {

		ExcelBuilderResult result = new ExcelBuilderResult();
		if(excelBuilderParam.getParam(ExcelBuilderParam.DETAIL_CONDITIONS) == null || ((Map<String, String>)excelBuilderParam.getParam(ExcelBuilderParam.DETAIL_CONDITIONS)).get("conditions") != null){
			// 列定义
			List<ExcelExportEntity> excelColumns = this.buildExcelTitle(excelBuilderParam);
			result.setExcelColumns(excelColumns);
		}
		if(excelBuilderParam.getParam(ExcelBuilderParam.DETAIL_CONDITIONS) == null || ((Map<String, String>)excelBuilderParam.getParam(ExcelBuilderParam.DETAIL_CONDITIONS)).get("detail") != null){
			// 数据组组建
			List<Map<String, Object>> excelCellValues = this.buildExcelCellValues(excelBuilderParam);
			result.setExcelCellValues(excelCellValues);
		}
		return result;
	}

	@SuppressWarnings("unchecked")
	private List<ExcelExportEntity> buildExcelTitle(ExcelBuilderParam excelBuilderParam) {

		Map<String, String> titleNames = (Map<String, String>) excelBuilderParam.getParam(ExcelBuilderParam.TITLE_NAMES);
		String indexYearMonth = (String)excelBuilderParam.getParam(ExcelBuilderParam.INDEX_YEAR_MONTH);
		List<ExcelExportEntity> excelColumns = new ArrayList<ExcelExportEntity>();

		// 第一部分:指标名称
		{
			ExcelExportEntity indexColumn = new ExcelExportEntity("指标", "indexCodeColumn");
			indexColumn.setNeedMerge(true);
			excelColumns.add(indexColumn);
		}
		// 第二部分:机构权重系系列
		{
			List<String> weightDeparts = null;
			weightDeparts = (List<String>) excelBuilderParam.getParam(ExcelBuilderParam.TITLE_WEIGHT_DEPARTS);
			for (String wDepart : weightDeparts) {
				ExcelExportEntity weightDepartColumn = new ExcelExportEntity(titleNames.get(wDepart), ExcelBuilderUtils.buildWeightKey(wDepart));
				weightDepartColumn.setNeedMerge(true);
				excelColumns.add(weightDepartColumn);
			}

		}
		// 第三部分: 考核项(指标值)
		{
			ExcelExportEntity indexValueColumns = new ExcelExportEntity("指标值", "indexValueColumns");
			indexValueColumns.setWidth(21);
			
			List<ExcelExportEntity> indexValueColumnsSubItems = new ArrayList<>();
			ExcelExportEntity indexValueColumnName = new ExcelExportEntity("考核项", "indexValueColumnsName");
			indexValueColumnName.setWidth(21);
			indexValueColumnsSubItems.add(indexValueColumnName);
			indexValueColumns.setList(indexValueColumnsSubItems);

			excelColumns.add(indexValueColumns);
		}
		// 第四部分: 考核机构
		{
			List<String> dataDeparts = null;
			dataDeparts = (List<String>) excelBuilderParam.getParam(ExcelBuilderParam.TITLE_DATA_DEPARTS);
			String month = indexYearMonth.substring(4);
			month = month.startsWith("0") ? month.substring(1) : month;
			for (String departCode : dataDeparts) {
				ExcelExportEntity departColumns = new ExcelExportEntity(titleNames.get(departCode), "v" + departCode);
				List<ExcelExportEntity> departColumnsSubItems = new ArrayList<>();
				departColumnsSubItems.add(new ExcelExportEntity("累计得分", "mothTotal"));
				departColumns.setList(departColumnsSubItems);
				excelColumns.add(departColumns);
			}
		}
		return excelColumns;
	}

	@SuppressWarnings("unchecked")
	private List<Map<String, Object>> buildExcelCellValues(ExcelBuilderParam excelBuilderParam) {
		//String currentMonth = EsDateUtils.getIndexYearMonth(new Date()).substring(4);
		List<Map<String, Object>> excelCellValues = new ArrayList<>();

		String indexYearMonth = (String)excelBuilderParam.getParam(ExcelBuilderParam.INDEX_YEAR_MONTH);
		DatagridVO datagridVO = (DatagridVO)excelBuilderParam.getParam(ExcelBuilderParam.DATAGRIDVO);
		String indexGroup = datagridVO.getIndexGroup();
		String dataType = (String) excelBuilderParam.getParam(ExcelBuilderParam.DATA_TYPE);
		//加载要考核的一线单位指标数据
		Map<String, List<TkIndexInfo>> indexs = indexDataLoader.loadIndexs(datagridVO,false);
		for(String indexCode : indexs.keySet()) {
			
			Map<String, Object> oneLineValues = new HashMap<>();
			
			String indexName = indexs.get(indexCode).get(0).getIndexName();
			
			// 第一部分:指标名称
			{
				oneLineValues.put("indexCodeColumn", indexName);
			}
			// 第二部分:机构权重系系列
			{
				List<String> weightDeparts = (List<String>) excelBuilderParam.getParam(ExcelBuilderParam.TITLE_WEIGHT_DEPARTS);
				Map<String, String> indexWeights = indexDataLoader.loadIndexWeights(indexYearMonth, indexGroup, indexCode, weightDeparts);
				for(String wDepartCode : weightDeparts) {
					oneLineValues.put(ExcelBuilderUtils.buildWeightKey(wDepartCode), indexWeights.get(wDepartCode) != null ? indexWeights.get(wDepartCode) : "-");
				}
				
			}
			// 第三部分: 考核项(指标值)
			{
				List<Map<String, Object>> indexValueColumns = new ArrayList<>();
				
				List<TkIndexInfo> indexInfoList = indexs.get(indexCode);
				for(TkIndexInfo info : indexInfoList) {
					
					Map<String, Object> indexValueColumnsSubItems = new HashMap<>();
					indexValueColumnsSubItems.put("indexValueColumnsName", info.getIndexValueName());
					
					indexValueColumns.add(indexValueColumnsSubItems);
				}
				
				oneLineValues.put("indexValueColumns", indexValueColumns);
			}
			// 第四部分: 考核机构 (要考虑一个部门两个被评分人情况)
			{
				List<String> dataDeparts = null;
				dataDeparts = (List<String>) excelBuilderParam.getParam(ExcelBuilderParam.TITLE_DATA_DEPARTS);
				for (String dataDepartCode : dataDeparts) {
					List<Map<String, Object>> departColumns = new ArrayList<>();
					
					String indexDepart = ExcelBuilderUtils.getIndexDepart(dataDepartCode);
					String indexScorer = dataDepartCode.contains("@")?ExcelBuilderUtils.getIndexScorer(dataDepartCode):null;
					List<TkIndexInfo> indexInfoList = indexs.get(indexCode);
					for(TkIndexInfo info : indexInfoList) {
						String indexValueCode = info.getIndexValueCode();
						TkIndexValueMirror indexValueMirror = null;
						Map<String, Object> departColumnsSubItems = new HashMap<>();
						indexValueMirror = indexDataLoader.loadMirrorIndexValues(indexYearMonth, indexValueCode, indexDepart, indexScorer,dataType);
						if(null == indexValueMirror) {
							departColumnsSubItems.put("mothTotal", "-");
						}else{
							QueryWrapper<TkIndexValueAttrs> attrQueryWrapper = new QueryWrapper<>();
							attrQueryWrapper.lambda().eq(TkIndexValueAttrs::getIndexValueCode,indexValueMirror.getIndexValueCode());
							attrQueryWrapper.lambda().eq(TkIndexValueAttrs::getAttrCode,"indexFormat");
							TkIndexValueAttrs tkIndexValueAttrs = tkIndexValueAttrsMapper.selectOne(attrQueryWrapper);
							departColumnsSubItems.put("mothTotal", DeptConstant.getPercent(indexValueMirror.getMTotal(),tkIndexValueAttrs.getAttrValue()));
						}
						departColumns.add(departColumnsSubItems);
					}
					oneLineValues.put("v" + dataDepartCode, departColumns);
				}
			}
			excelCellValues.add(oneLineValues);
		}
		
		return excelCellValues;
		
	}
}
